home *** CD-ROM | disk | FTP | other *** search
/ Adobe Graphics & Publishing SDK 1996 December / Adobe Graphics & Publishing SDK 1996 December.iso / mac / PageMaker 6.5 SDK Mac / SourceCode / C_Language / ExportF / Common / CONVERT.CPP next >
C/C++ Source or Header  |  1996-09-05  |  5KB  |  267 lines

  1. /*
  2.  *--- Convert.cpp -------------------------------------------------------
  3.  * Copyright (c) 1992-96 Adobe Systems, Inc.  All rights reserved.
  4.  *
  5.  * PageMaker plug-in.
  6.  *-----------------------------------------------------------------------
  7.  */
  8.  
  9. /* ---------------- PageMaker SDK include files ---------------- */
  10. #include "PMPlugin.h"
  11.  
  12. /* ---------------- Plug-in include files ---------------- */
  13. #include "Convert.h"
  14.  
  15. /* ---------------- Cross-platform include files ---------------- */
  16. #include <string.h>
  17. #include <stdio.h>
  18.  
  19. /* ---------------- Platform specific include files ---------------- */
  20. #ifdef MACINTOSH
  21.     #ifndef powerc
  22.         #include <A4Stuff.h>
  23.         #include <SetUpA4.h>
  24.     #endif
  25.     #include <Packages.h>
  26. #else
  27.     #include <Windows.h>
  28. #endif
  29.  
  30. /* ---------------- Type definitions ---------------- */
  31.  
  32. /* ---------------- Definitions ---------------- */
  33.  
  34. /* ---------------- PageMaker (New)SDK include files ---------------- */
  35. #include "CICommandsAndQueries.h"
  36.  
  37. /* ---------------- Function declarations ---------------- */
  38. void XltIntlDecimal(char * buffer);
  39. char DecPtChar(void);
  40. PMXErr SizeToTwips(float srcSize, long *Twips);
  41. PMXErr TwipsToSize(long Twips, float *destSize);
  42. PMXErr SizeToString(long srcSize, char *destStr, size_t *destStrLen);
  43.  
  44.  
  45. /* ---------------- Globals ---------------- */
  46. kUnits    gSrcType, gDestType;
  47. PMBool      gLocalise;
  48. extern sPMParamBlockPtr thePB;
  49.  
  50. PMXErr Convert(kUnits srcType, float srcSize, kUnits destType, char *destStr, 
  51.                 float *destSize, size_t *destStrLen, PMBool localise)
  52. {
  53.     PMXErr    result = 1;
  54.     long    lTwips;
  55.     float    dSize;
  56.     size_t    sDestStrLen;
  57.     
  58. #if __MWERKS__ && __MC68K__
  59.     long oldA4 = SetCurrentA4();
  60.     RememberA4();
  61. #endif
  62.  
  63.     gLocalise = localise;
  64.     
  65.     gSrcType = srcType; gDestType = destType;
  66.  
  67.     result = SizeToTwips(srcSize, &lTwips);
  68.     if (result != 0)
  69.         goto onErr;
  70.  
  71.     result = TwipsToSize(lTwips, &dSize);
  72.     if (result != 0)
  73.         goto onErr;
  74.  
  75.     *destSize = dSize;
  76.     
  77.     result = SizeToString(lTwips, destStr, &sDestStrLen);
  78.     if (result != 0)
  79.         goto onErr;
  80.     
  81.     *destStrLen = sDestStrLen;
  82.     
  83. #if __MWERKS__ && __MC68K__
  84.     SetA4(oldA4);
  85. #endif
  86.  
  87. onErr:
  88.     return result;
  89. }
  90.  
  91. PMXErr TwipsToSize(long Twips, float *destSize)
  92. {
  93.     PMXErr    result = 0;
  94.     float    divisor;
  95.     
  96.         switch (gDestType) {
  97.             case kInches:
  98.                 divisor = kTwipsPerInch/10;
  99.                 break;
  100.                 
  101.             case kInchesDecimal:
  102.                 divisor = kTwipsPerInch;
  103.                 break;
  104.                 
  105.             case kMillimeters:
  106.                 divisor = kTwipsPerMillimeter;
  107.                 break;
  108.                 
  109.             case kPicas:
  110.                 divisor = kTwipsPerPica;
  111.                 break;
  112.                 
  113.             case kCiceros:
  114.                 divisor = kTwipsPerCicero;
  115.                 break;            
  116.                     
  117.             case kTwips:
  118.                 divisor = 1;
  119.                 break;
  120.  
  121.             default:
  122.                 result = -1; // unit is not handled. We do not handle dontcare as well.
  123.             break;
  124.         }
  125.  
  126.     *destSize = (float)(Twips / divisor);
  127.  
  128.     return result;
  129. }
  130.  
  131. PMXErr SizeToTwips(float srcSize, long *Twips)
  132. {
  133.     PMXErr    result = 0;
  134.     float    divisor;
  135.     
  136.         switch (gSrcType) {
  137.             case kInches:
  138.                 divisor = kTwipsPerInch/10;
  139.                 break;
  140.  
  141.             case kInchesDecimal:
  142.                 divisor = kTwipsPerInch;
  143.                 break;
  144.                 
  145.             case kMillimeters:
  146.                 divisor = kTwipsPerMillimeter;
  147.                 break;
  148.                 
  149.             case kPicas:
  150.                 divisor = kTwipsPerPica;
  151.                 break;
  152.                 
  153.             case kCiceros:
  154.                 divisor = kTwipsPerCicero;
  155.                 break;            
  156.  
  157.             case kTwips:
  158.                 divisor = 1;
  159.                 break;
  160.                                     
  161.             default:
  162.                 result = -1;
  163.             break;
  164.         }
  165.  
  166.     *Twips = (long)(srcSize * divisor);
  167.     
  168.     return result;
  169. }
  170.  
  171. PMXErr SizeToString(long srcSize, char *destStr, size_t *destStrLen)
  172. {
  173.     PMXErr    result = 0;
  174.     struct {
  175.         long    dSize;
  176.         short    dUnit;
  177.     } convValue;
  178.     
  179.     convValue.dSize = srcSize;
  180.     convValue.dUnit = gDestType;
  181.     
  182.     memset(destStr, 0, 256);
  183.     
  184.     result = PBBinQueryWithParms( thePB, pm_getconverttwips, kXRSPointer, &convValue, 
  185.                                 sizeof(convValue), kXRSPointer, destStr, 256 );
  186.     if (result != CQ_SUCCESS)
  187.         goto onErr;
  188.     /*
  189.     short    x;
  190.     long    tempLong;
  191.     char    tempString[128], smallStr[128];
  192.  
  193.     destStr[0] = 0;
  194.  
  195.     if ( srcSize < 0 ) {
  196.         strcat( destStr, "-" );
  197.         srcSize *= -1;
  198.     }
  199.  
  200.     tempLong = srcSize;
  201.     sprintf( tempString, "%ld", tempLong );
  202.     strcat( destStr, tempString );
  203.  
  204.     tempString[0] = 0;
  205.     for ( x = 0; x < precision; x++ ) {
  206.         srcSize *= 10;
  207.         tempLong *= 10;
  208.         srcSize -= tempLong;
  209.         if ( srcSize == 0 ) break;
  210.         tempLong = srcSize;
  211.         sprintf( smallStr, "%ld", tempLong );
  212.         strcat( tempString, smallStr );
  213.     }
  214.                 
  215.     if ( strlen(tempString) ) {
  216.         strcat( destStr, "." );
  217.         strcat( destStr, tempString );
  218.     }
  219.     */
  220.     
  221.     if (gLocalise)
  222.         XltIntlDecimal(destStr);
  223.  
  224.     *destStrLen = strlen(destStr);
  225.     
  226. onErr:
  227.     return result;
  228. }
  229.  
  230. void XltIntlDecimal(char * buffer)
  231. {
  232.  
  233.    static char    decimalPt = 0;
  234.  
  235.     if (!decimalPt)
  236.         decimalPt = DecPtChar();
  237.  
  238.  
  239.    if ( decimalPt != '.' ) {
  240.       if (buffer = strchr(buffer, '.'))
  241.             *buffer = decimalPt;
  242.    }
  243. }
  244.  
  245.  
  246. char DecPtChar(void)
  247. {
  248. #ifdef MACINTOSH
  249.     char decPt;
  250.  
  251.     Intl0Hndl    iHndl;
  252.     iHndl = (Intl0Hndl)IUGetIntl(0);
  253.     if (iHndl)
  254.         decPt = (**iHndl).decimalPt;
  255.     else
  256.         decPt = '.';    // default value
  257.  
  258.     return decPt;
  259. #endif
  260. #ifdef WINDOWS
  261.     char decPt[2];
  262.  
  263.     GetProfileString("intl", "sDecimal", ".", decPt, sizeof(decPt));
  264.  
  265.     return decPt[0];
  266. #endif
  267. }